//
// Created by denglibin on 2021/2/4.
//

#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
#include <SDL_image.h>
#define true 1
#define false 0
static const int WIN_WIDTH = 1920;
static const int WIN_HEIGHT = 1080;
static SDL_Window *win; //窗口指针
static SDL_Surface  *winSurface;
static SDL_Renderer *winRenderer;
static int quit = 0;
static SDL_Texture *circleTexture;
static  SDL_Texture *bgTexture;
static int const SPOTS_NUM = 500; //点的数量

/**
 * 斑点结构体
 */
typedef struct{
    int x;
    int y;
    int r;
    int speed;
    int alpha;
}Spot;
/**
 * 加载图片 返回纹理
 * @param file
 * @return 纹理
 */
static SDL_Texture* loadImage(const char * file){
    SDL_Texture * texture = IMG_LoadTexture(winRenderer, file);
    if(texture == NULL){
        printf("IMG_LoadTexture ERROR:%s\n", SDL_GetError());
        exit(1);
    }
    return texture;
}

/**
 * 创建并显示窗口
 */
static void createAndShowWin(){
    win = SDL_CreateWindow("demo4", SDL_WINDOWPOS_UNDEFINED,
                           SDL_WINDOWPOS_UNDEFINED,
                           WIN_WIDTH, WIN_HEIGHT,
                           SDL_WINDOW_SHOWN);
    if(win == NULL){
        printf("SDL_CreateWindow ERROR:%s\n", SDL_GetError());
        exit(1);
    }
    //获取窗口显示层
    winSurface = SDL_GetWindowSurface(win);

    //主窗口渲染器
    winRenderer = SDL_CreateRenderer(
            win,
            -1,  //序号 -1表示第一个
            SDL_RENDERER_ACCELERATED//使用硬件加速
            | SDL_RENDERER_PRESENTVSYNC); //和刷新频率同步
}

/**
 * 根据图片初始化纹理
 */
static void initTexture(){
    SDL_Surface *circleSurface = SDL_LoadBMP("img/circle.bmp");
    SDL_Surface *bgSurface = SDL_LoadBMP("img/black.bmp");
    SDL_SetColorKey(circleSurface, SDL_TRUE, SDL_MapRGB(circleSurface->format, 127, 127, 127)); //将指定颜色设为透明
    circleTexture = SDL_CreateTextureFromSurface(winRenderer,circleSurface);
    SDL_SetTextureBlendMode(circleTexture, SDL_BLENDMODE_BLEND);
    SDL_SetTextureColorMod(circleTexture, 0, 255, 0); //设置颜色 绿色

    bgTexture = SDL_CreateTextureFromSurface(winRenderer,bgSurface);

    SDL_SetTextureBlendMode(bgTexture, SDL_BLENDMODE_BLEND);
    SDL_SetTextureAlphaMod(bgTexture, 60); //修改alpha值，实现明暗效果， 使下落的点带上尾巴

}

/**
 * 渲染纹理
 * @param texture
 * @param srcPos
 * @param distPos
 */
static void renderTexture(SDL_Texture * texture,  SDL_Rect *srcPos,  SDL_Rect *distPos){

    //获取纹理宽高
   // SDL_QueryTexture(texture, NULL, NULL, &pos.w, &pos.h);
    //将纹理复制到渲染器上
    SDL_RenderCopy(winRenderer, //渲染器
                   texture,
                   srcPos,
                   distPos);
}
/**
 * 关闭资源
 */
static void close(){
    // 清理资源
    SDL_DestroyTexture(bgTexture);
    SDL_DestroyTexture(circleTexture);
    SDL_FreeSurface(winSurface);
    SDL_DestroyRenderer(winRenderer);
    SDL_DestroyWindow(win);
    SDL_Quit();
}
/**
 * 事件处理
 * @param e
 */
static void keyDownEvent(SDL_Event* e){
    switch (e->key.keysym.sym) {
        case SDLK_ESCAPE:
            quit = true;
            break;
        case SDLK_DOWN:
            break;
        case SDLK_UP:
            break;
        default:
            break;
    }

}




/**
 * 初始化
 */
static void init(){
   createAndShowWin();
   initTexture();

}



/**
 * 绘制斑点
 * @param spot
 */
static void drawSpot(Spot* spot){
    SDL_Rect  srcPos; //位置
    SDL_Rect  distPos; //位置
    srcPos.x = 0;
    srcPos.y = 0;
    srcPos.w = spot->r;
    srcPos.h = spot->r;

    distPos.x = spot->x;
    distPos.y = spot->y;
    distPos.w = srcPos.w;
    distPos.h = srcPos.h;
    SDL_SetTextureAlphaMod(circleTexture, spot->alpha);
    renderTexture(circleTexture, &srcPos, &distPos);
}
/**
 * 演示
 */
static void showDemo(){
   Spot spots[SPOTS_NUM];
   int i;
   for(i = 0; i < SPOTS_NUM; i++){
       spots[i].x = rand() % WIN_WIDTH;
       spots[i].y = rand() % WIN_HEIGHT;
       spots[i].speed = rand() % 2 + 1;
       spots[i].r = rand() % 3  + 2 ;
       spots[i].alpha = rand() % 125 + 70;
   }

    while (!quit){
        renderTexture(bgTexture, NULL, NULL); //绘制背景
        for(i = 0; i < SPOTS_NUM; i++){
           spots[i].y +=  spots[i].speed;
           if(spots[i].y >= WIN_HEIGHT){
               spots[i].y = 0;
           }
           drawSpot(&spots[i]);
        }
        SDL_RenderPresent(winRenderer); //展示
        SDL_Delay(20);

    }
}
/**
 * 演示
 */
static void demo(){

    SDL_Thread *thread = SDL_CreateThread((SDL_ThreadFunction) showDemo, "demo", NULL); //在单独的线程中执行
    if ( thread == NULL ) {
        fprintf(stderr, "无法创建线程： %s/n", SDL_GetError());
        return;
    }
   // SDL_WaitThread(thread, NULL);

}

/**
 * 事件循环
 */
static void eventLoop(){
    SDL_Event  event;
    while (!quit)
    {//主消息循环
        SDL_WaitEvent(&event);

        switch (event.type)
        {
            //用户从菜单要求退出程序
            case SDL_QUIT:
                quit = 1;
                break;
            case SDL_KEYDOWN:
                keyDownEvent(&event);
                break;
            default:
                break;
        }
    }
}

int main4(int argc, char* argv[]){

    init();
    demo();
    eventLoop();
    close();
    return 0;
}